package com.gc.web.controller.business;

import java.util.List;
import java.util.Arrays;
import com.gc.common.base.Result;
import com.gc.common.support.Convert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.MemoNote;
import com.gc.business.service.IMemoNoteService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 备忘录 控制层
 * 
 * @author liujinteng
 * @date 2020-12-02
 */
@Slf4j
@Controller
@RequestMapping("/business/memoNote")
public class MemoNoteController extends BaseController {
    private String prefix = "business/memoNote";
	
	private final IMemoNoteService memoNoteService;

	@Autowired
	public MemoNoteController(IMemoNoteService memoNoteService) {
		this.memoNoteService = memoNoteService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:memoNote:view")
	@GetMapping
	public String memoNote(){
	    return prefix + "/memoNote";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{memoId}")
	public String edit(@PathVariable("memoId") Long memoId, ModelMap mmap){
		MemoNote memoNote = memoNoteService.getById(memoId);
		mmap.put("memoNote", memoNote);
		return prefix + "/edit";
	}
	
	/**
	 * 查询备忘录列表
	 */
	@RequiresPermissions("business:memoNote:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(MemoNote memoNote){
		startPage();
        List<MemoNote> list = memoNoteService.list(memoNote);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存备忘录
	 */
	@RequiresPermissions("business:memoNote:add")
	@Log(title = "备忘录", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(MemoNote memoNote){
		memoNoteService.save(memoNote);
		return Result.success();
	}

	/**
	 * 修改保存备忘录
	 */
	@RequiresPermissions("business:memoNote:edit")
	@Log(title = "备忘录", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(MemoNote memoNote){
		memoNoteService.updateById(memoNote);
		return Result.success();
	}
	
	/**
	 * 删除备忘录
	 */
	@RequiresPermissions("business:memoNote:remove")
	@Log(title = "备忘录", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		memoNoteService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出备忘录列表
	 */
	@RequiresPermissions("business:memoNote:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(MemoNote memoNote){
		List<MemoNote> list = memoNoteService.list(memoNote);
		ExcelUtil<MemoNote> util = new ExcelUtil<>(MemoNote.class);
		util.exportExcel(list, "memoNote");
		return Result.success();
	}
}
